package ihm.views.preferences;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import ihm.models.ATKmodel;
import ihm.views.options.ATKjOptionPane;

import javax.swing.JOptionPane;

import systems.languages.MultiLanguages;

public class LanguageBox implements ActionListener{

  private static String[] languageList = MultiLanguages.getLanguagesList().toArray(new String[ MultiLanguages.getLanguagesList().size()]);
  private static String currentLang = MultiLanguages.getInstance().getLanguageName();
  private static int currentLangIndex=0;
  private String newLanguage = null;
  private ATKmodel model;
  
  public LanguageBox(ATKmodel model)
  {
    this.model = model;
  }

  private void findCurrentLang()
  {
    for(String lang : languageList)
      if(!lang.equalsIgnoreCase(currentLang))
        currentLangIndex++;
      else
        return;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    findCurrentLang();
    newLanguage = (String)ATKjOptionPane.showInputDialog(null,
        MultiLanguages.getProperties("dialog.SelectLangage.text"),
        MultiLanguages.getProperties("dialog.SelectLangage.title"),
        JOptionPane.PLAIN_MESSAGE,
        null,
        languageList,
        languageList[currentLangIndex]);
    if(newLanguage != "null" && !newLanguage.equalsIgnoreCase(currentLang))
    {
      currentLang = newLanguage;
      //System.out.println("Nouvelle langue : "+newLanguage);
      model.setLanguage(newLanguage);
    }
  }

}